package cn.jhc.booklist;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by luyanfei on 16-2-26.
 */
public class BookDescFragment extends Fragment {
    public static final String BOOK_INDEX = "book index";
    public static final int BOOK_INDEX_NOT_SET = -1;
    private String[] bookDescriptions;
    private TextView bookDescriptionTextView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View viewHierarchy = inflater.inflate(R.layout.book_desc, container, false);

        bookDescriptions = getResources().getStringArray(R.array.bookDescs);
        bookDescriptionTextView = (TextView) viewHierarchy.findViewById(R.id.text_book_desc);

        //解决动态添加Fragment无法直接调用setBook的问题
        Bundle args = getArguments();
        int bookIndex = args != null ?
                args.getInt(BOOK_INDEX, BOOK_INDEX_NOT_SET) : BOOK_INDEX_NOT_SET;
        if(bookIndex != BOOK_INDEX_NOT_SET)
            setBook(bookIndex);

        return viewHierarchy;
    }

    public void setBook(int bookIndex) {
        String description = bookDescriptions[bookIndex];
        bookDescriptionTextView.setText(description);
    }
}
